const fs = require('fs')
const path = require('path')
const gm = require('gm')
const tesseract = require('node-tesseract')

async function tryPraseCode(path) {
  return new Promise((resolve, reject) => {
    tesseract.process(path, { psm: 6 }, function(err, text) {
      if (err) {
        reject(err)
      } else {
        resolve(text.trim())
      }
    })
  })
}

async function findContours() {
  const testCaptchaPath = './test_captcha.jpeg'
  gm('./test.jpeg')
    .threshold(55, true)
    // .deconstruct()
    // .sharpen(2)
    // .trim()
    .repage(50, 70, 0, 0)
    // .channel('Black')
    .write('test_captcha.jpeg', err => {
      if (err) console.log(err)
    })
  const result = await tryPraseCode(testCaptchaPath)
  console.log(result)
}

findContours()
